<?php
namespace app\index\controller;

use think\Controller;
use auth\Auth;
use think\Db;
use think\Request;

class Guanli extends Controller
{
   //查询
   public function index(){
    
		$data=db('guanli')
			  ->join('auth_group','auth_group.id=f_guanli.gid')
			  ->field('f_guanli.*,jname')
			  ->order('f_guanli.id desc')
		      ->paginate(7);
		    
			//分页
            $page = $data->render();
         //halt($data);
    
	    //计算多少条数据
		 $count=count(db('guanli')->select());
		 
		return view('',['data'=>$data,'tiao'=>$count,'page'=>$page]);
   }
    
  //添加

    /**
     * @return \think\response\View|void
     */
    public function guanliadd(){
        if(request()->isAjax())
        {
            $gname=input('gname');
            $phone=input('phone');
            $repwd=md5(input('repwd'));
            $jid=input('jid');
            $picture=input('picture');
            Db::name('guanli')->insert([
                'phone'=>$phone,
                'gname'=>$gname,
                'pwd'=>$repwd,
                'gid'=>$jid,
                'head_portrait'=>$picture,
            ]);
            $uid = Db::name('guanli')->getLastInsID();
            $access=db('auth_group_access')->insert(['uid'=>$uid,'group_id'=>$jid]);
            if($access){
                return 1;
            }
            else
            {
                return 0;
            }
        }
        else
        {
            $jiaose=db('auth_group')->select();
            return view('guanliadd',['jiaose'=>$jiaose]);
        }

  }

   //删除
   public function delete(){
		$id=input('id');
		//获取当前域名
        $request = Request::instance();
        $domain=$request->domain();
        $rs=db('guanli')->where('id',$id)->find();
        $head_portrait=ROOT_PATH.'upload/head/'.$rs['head_portrait'];
        @unlink($head_portrait);
        $del=db('guanli')->where('id',$id)->delete();
        if($del){
            return array('code'=>1,'msg'=>"删除成功");
        }else{
            return array('code'=>0,'msg'=>"删除失败");
        }
     }

    public function doUpload()
    {
        $file = request()->file('file');
        //echo $file;die;
        $fileName=md5(uniqid(md5(microtime(true)),true));
        $imgUrl= ROOT_PATH . '/upload/head/';
        // 移动到框架应用根目录/uploads/ 目录下
        $info = $file->move($imgUrl,$fileName);
        $error = $file->getError();
        if ($info) {
            // 成功上传后 获取上传信息
            $url= $info->getSaveName();
            return json(['code' => 1, 'msg' => '上传成功!', 'url' =>$url]);
        } else {
            // 上传失败获取错误信息
            return json(['code' => 0, 'msg' => $file->getError(), 'url' => '']);
        }

    }

    //修改
     public function update(){
		$id=input('id');
         if(request()->isAjax()) {
             $phone=input('phone');
             $gname=input('gname');
             $jid=input('jid');
             $picture=input('picture');
             if($picture!='')
             {
                 //$file = request()->file('head_portrait');//halt($file);
                 //获取当前域名
                 $rs = db('guanli')
                     ->join('auth_group', 'auth_group.id=f_guanli.gid')
                     ->where('f_guanli.id', $id)
                     ->find();
                 $request = Request::instance();
                 $domain = $request->domain();
                 $head_portrait =ROOT_PATH. 'upload/head/' . $rs['head_portrait'];
                 @unlink($head_portrait);
                 clearstatcache();
                 //$info = $file->move(ROOT_PATH . 'public' . DS . '/upload/head/');
                 $res = db('guanli')
                     ->where('id', $id)
                     ->update([
                         'phone' => $phone,
                         'gname' => $gname,
                         'gid' => $jid,
                         'head_portrait' => $picture,
                     ]);
                 $access = db('auth_group_access')->where('uid', $id)->update(['group_id' => $jid]);
                 if ($res) {
                     return 1;
                 } else {
                     return 0;
                 }
             }
             else
             {
                 $res = db('guanli')
                     ->where('id', $id)
                     ->update([
                         'phone' => $phone,
                         'gname' => $gname,
                         'gid' => $jid,
                     ]);
                 $access = db('auth_group_access')->where('uid', $id)->update(['group_id' => $jid]);
                 if ($res) {
                     return 1;
                 } else {
                     return 0;
                 }
             }

         }
         else
         {
             $rs = db('guanli')
                 ->join('auth_group', 'auth_group.id=f_guanli.gid')
                 ->where('f_guanli.id', $id)
                 ->find();
             //halt($rs);
             $jiaose = db('auth_group')->where('jname', 'not in', $rs['jname'])->select();
             return view('update', ['rs' => $rs, 'jiaose' => $jiaose,'hid'=>$id]);
         }
		
	 }

    //修改密码
	  public function modPassWord(){
          if(request()->isAjax())
          {
              $id=input('id');
              //echo $id.'1';die;
              $data = input('repwd');
              $res = db('guanli')
                  ->where('id', $id)
                  ->update(['pwd' => md5($data)]);
              if ($res)
              {
                  return 1;
              }
              else
              {
                  return 0;
              }
          }
          else
          {
              return view('modpassword');
          }
	  }

}
		